function DOMSizeInitSet() {
    $('#wrap-box').height($(window).height() - 48)
}

// 防抖
/*
 ** @fn 要执行的函数
 ** @delay 间隔市场
 ** @immediate 是否立即触发一次
 */
function debounce(fn, delay, immediate) {
    if (typeof fn !== 'function') {
        throw new TypeError('need a function arguments')
    }
    var timer = null
    return function () {
        var context = this
        var args = arguments
        // 每次这个返回的函数被调用，就清除定时器，以保证不执行 fn
        clearTimeout(timer)
        if (immediate) { //当返回的函数被第一次调用后 (连续操作开始，函数立即执行)
            var callNow = !timer
            if (callNow) fn.apply(context, args)
            timer = setTimeout(function () {
                timer = null
            }, delay)
        } else { // 当返回的函数被最后一次调用后（也就是用户停止了某个连续的操作），         
            // 再过 delay 毫秒就执行 fn
            timer = setTimeout(function () {
                fn.apply(context, args)
            }, delay)
        }
    }
}

// 节流
function throttle(fn, delay) {
    if (typeof fn !== 'function') {
        throw new TypeError('need a function arguments')
    }
    var timer = null
    var startTime = Date.now()
    // console.log(startTime)
    return function () {
        var currTime = Date.now()
        var remaining = delay - (currTime - startTime)
        var context = this
        var args = arguments
        clearTimeout(timer)
        if (remaining <= 0) {
            fn.apply(context, args);
            startTime = Date.now()
        } else {
            timer = setTimeout(function () {
                fn.apply(context, args);
            }, remaining)
        }
    }
}

// 获取浏览器参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}
//替换指定传入参数的值,name,value为新值,修改参数并且不发送请求
function changeURLParam(name, value) {
    var url = document.URL, resultUrl = ''
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
    var r = window.location.search.substr(1).match(reg);
    var replaceText = name + '=' + value;
    if (r != null) {
        var tmp = url.replace(unescape(name + '=' + r[2]), replaceText);
        resultUrl = (tmp);
    } else {
        if (url.match('[\?]')) {
         resultUrl = url + '&' + replaceText;
        }
        else {
         resultUrl = url + '?' + replaceText;
        }
    }
    history.replaceState(null, null, resultUrl)
 }

$(function () {
    if ($('#wrap-box').length > 0) {
        DOMSizeInitSet()
        $(window).resize(DOMSizeInitSet)
    }

    var returnTopBtn = $('.J-return-top');

    if (returnTopBtn.length > 0) {
        returnTopBtn.click(function () {
            $('body,html').animate({
                scrollTop: 0
            }, 1000)
        })
    }

    $(window).scroll(debounce(returnTop, 100, false)) // 回到顶部函数调用，加入防抖程序，优化性能

    function returnTop() {
        var _scrollTop = $(window).scrollTop();
        if (returnTopBtn.length > 0) {
            if (_scrollTop > $(window).height()) {
                $('.J-return-top').addClass('show')
            } else {
                $('.J-return-top').removeClass('show')
            }
        }
    }



})